//
//  MCChooseForPlatformScreeningView.swift
//  StoreManage
//
//  Created by MC on 2018/2/5.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCChooseForPlatformScreeningView: UIView {

    var dataArray : NSArray = NSArray() {
        didSet {
            self.colleciontView.reloadData()
        }
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        sendNetworking_getStoreBrandList()
        self.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(self)
        }
        
        self.addSubview(whiteView)
        whiteView.snp.remakeConstraints { (make) ->Void in
            make.left.right.top.equalTo(bgView)
            make.height.equalTo(306)
        }

        
        whiteView.addSubview(colleciontView)
        colleciontView.snp.remakeConstraints { (make) ->Void in
            make.left.right.top.equalTo(whiteView)
            make.height.equalTo(250)
        }
        
        let margin : CGFloat = 35
        let buttonW = (MCScreenWidth - 3 * margin) / 2
        
        whiteView.addSubview(resetButton)
        resetButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(margin)
            make.bottom.equalTo(-10)
            make.size.equalTo(CGSize.init(width: buttonW, height: 36))
        }
        
        whiteView.addSubview(sureButton)
        sureButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-margin)
            make.bottom.equalTo(-10)
            make.size.equalTo(CGSize.init(width: buttonW, height: 36))
        }
    }
    
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func dismiss() {
        self.removeFromSuperview()
    }

    func sendNetworking_getStoreBrandList() {
        let para = [ : ] as [String : Any]
        
        MCNetwork_post(url: JAVA_getStoreBrandList, para: para as [String : AnyObject], vc: nil,hudSuperView: nil,isHiddenTabbar: true, success: { (response) in
            
            let arrayM = NSMutableArray()
            
            let dataArray = response["data"] as? NSArray ?? []
            for dict in dataArray {
                let model = MCChooseForPlatformScreeningModel()
                model.setValuesForKeys(dict as? [String:Any] ?? [:])
                arrayM.add(model)
            }
            self.colleciontView.reloadData()
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    
    // MARK: - Setter & Getter
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.init(white: 0, alpha: 0.4)
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(dismiss))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    lazy var whiteView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    }()

    
    private lazy var colleciontView: UICollectionView = {
        
        //  68  97
        let layout = UICollectionViewFlowLayout.init()
        layout.scrollDirection = UICollectionViewScrollDirection.vertical
        
        let margin = 15
        let itemWidth : CGFloat = (MCScreenWidth - CGFloat(margin * 2 + 15 * 2)) / 3
        let itemHeight : CGFloat = 30
        
        layout.sectionInset = UIEdgeInsetsMake(10, 15, 10, 15)
        
        layout.itemSize = CGSize.init(width: itemWidth, height: itemHeight)
        layout.minimumLineSpacing = CGFloat(margin)
        layout.minimumInteritemSpacing = CGFloat(margin)
        
        let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.register(MCChooseForPlatformScreeningCell.classForCoder(), forCellWithReuseIdentifier: "cell")
        return collectionView
    }()
    
    lazy var resetButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 3
        button.backgroundColor = UIColor.MCAssist
        button.setTitle("重置", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.titleLabel?.font = MCFont(14)
        return button
    }()
    
    lazy var sureButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 3
        button.backgroundColor = UIColor.MCMain
        button.setTitle("筛选", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.titleLabel?.font = MCFont(14)
        return button
    }()

}

extension MCChooseForPlatformScreeningView : UICollectionViewDelegate,UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MCChooseForPlatformScreeningCell
        
        for view in cell.contentView.subviews {
            view.removeFromSuperview()
        }
        
        cell.model = dataArray[indexPath.row] as! MCChooseForPlatformScreeningModel

        return cell
    }
}
